
<?php

class Inbound_InboundDetailController extends Zend_Controller_Action
{
	protected $_inboundDetail;
	protected $_inbound;
	
	public function init()
	{
		//tạo đối tượng chính của module
		$this->_inboundDetail = new Admin_Model_InboundDetail();
		$this->_inbound = new Admin_Model_InboundDetail();
	}
	
	//ajax check số dòng có vược quá giới hạn không 
	//dùng khi đang tạo phiếu
	/*protected function ajaxCheckRowCountAction()
	{
		//layout
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		
		//dem so dòng
		$row_count = $this->_inboundDetail->countPrintRow ( $inbound_details );
		
		//trả kết quả
		echo $row_count > 20?0:1;
	}*/
	
    //ajax load nhóm hàng hóa theo nhóm kho đang chọn
    //dùng cho báo cáo 
	protected function ajaxLoadProductGroupAction()
	{
		//layout
		$this->_helper->layout()->disableLayout();
		
		//lay tham so
		$request=$this->_request;
		$product_group_parent_group_code=$request->getParam('product_group_parent_group_code','');
		$product_group_code=$request->getParam('product_group_code','');

		//lay kho	
		$product_group = new Admin_Model_ProductGroup ();
		$options = $product_group->fetchOptions ( $product_group->fetchGroups ($product_group_parent_group_code) );
				
		//truyen ra view
		$this->view->array=array(""=>"- Chọn -")+$options;
		$this->view->product_group_code=$product_group_code;
	}
	
	//ajax load danh sách thủ kho theo kho đang chọn 
	protected function ajaxLoadWarehouseManageAction()
	{
		//layout
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		
		//lay tham so
		$request=$this->_request;
		$warehouse_code=$request->getParam('warehouse_code');

		//lay kho
		$warehouse=new Admin_Model_Warehouse();
		$warehouse->fetchRowAllBase(" warehouse_code='$warehouse_code' ");
				
		//chọn kho
		echo $warehouse->warehouse_manage_user;
	}
	
	//ajax load sp theo nhom sp
	//dùng cho báo cáo
	public function ajaxLoadProductAction()
	{
		//layout
		$this->_helper->layout()->disableLayout();
		
		//lay tham so
		$request=$this->_request;
		$product_product_group_code=$request->getParam('product_product_group_code');
		$product_code=$request->getParam('product_code');
		
		//lay nhom sp con
		$product_group=new Admin_Model_ProductGroup();
		$product_groups=$product_group->fetchObjects($product_group->fetchGroups($product_product_group_code));
		$string="'$product_product_group_code'";
		foreach ($product_groups as $product_group)
			$string.=",'{$product_group->product_group_code}'";
		
		//lay sp
		$product=new Admin_Model_Product();
		$where="1";
		if($product_product_group_code!="")
			$where.=" AND product_product_group_code IN ($string) ";
		$products=$product->fetchAllBase($where,"product_code ASC");
		
		//truyen ra view
		$this->view->array=array(""=>"- Chọn -")+$product->toArray2($products,"product_code",'$row->product_code - $row->product_name');
		$this->view->product_code=$product_code;
	}
	 
	//ajax load vị trí kho theo kho
	public function ajaxLoadLocationAction()
	{
		//layout
		$this->_helper->layout()->disableLayout();
		
		//lay tham so
		$request=$this->_request;
		$location_warehouse_code=$request->getParam('location_warehouse_code');
		$location_code=$request->getParam('location_code');

		//lay vi tri kho
		$location=new Admin_Model_Location();
		$locations=$location->fetchAllBase("location_warehouse_code='$location_warehouse_code'");
		
		//truyen ra view
		$this->view->array=array(""=>"- Chọn -")+$location->toArray2($locations,"location_code","\$row->location_code - \$row->location_name");
		$this->view->location_code=$location_code;
	}
	
	//ajax load đơn vị tính theo sp đang chọn
	public function ajaxLoadUnitAction()
	{
		//layout
		$this->_helper->layout()->disableLayout();
		
		//lay tham so
		$request=$this->_request;
		$product_code=$request->getParam('product_code');
		$unit_code=$request->getParam('unit_code');		

		//lay vi tri kho
		$unit=new Admin_Model_Unit();
		$sql="
		SELECT *
		FROM view_unit
		";
		$units=$unit->queryBase($sql);
		
		//truyen ra view
		$this->view->array=array(""=>"- Chọn -")+$unit->toArray2($units,"unit_code","unit_name");
		$this->view->unit_code=$unit_code;
	}
}